#!/usr/bin/env python3
"""
CSVHeaderManager 功能演示
"""

import csv
import os

def demo_csv_header_manager():
    """演示CSVHeaderManager的功能"""
    print("=== CSVHeaderManager 功能演示 ===")
    
    # 读取示例文件
    sample_file = "sample_no_header.csv"
    if not os.path.exists(sample_file):
        print(f"示例文件 {sample_file} 不存在")
        return
    
    print(f"原始文件内容:")
    with open(sample_file, 'r', encoding='utf-8') as f:
        content = f.read()
        print(content)
    
    # 演示1: 添加指定列名
    print("\n演示1: 添加指定列名")
    output_file1 = "sample_with_custom_headers.csv"
    
    # 手动模拟CSVHeaderManager的功能
    with open(sample_file, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        rows = list(reader)
    
    column_names = ["ID", "Name", "Age", "Occupation", "Salary"]
    
    with open(output_file1, 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(column_names)
        for row in rows:
            writer.writerow(row)
    
    print(f"输出文件: {output_file1}")
    with open(output_file1, 'r', encoding='utf-8') as f:
        content = f.read()
        print(content)
    
    # 演示2: 自动生成列名
    print("\n演示2: 自动生成列名")
    output_file2 = "sample_with_auto_headers.csv"
    
    with open(sample_file, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        rows = list(reader)
    
    # 自动生成列名
    num_columns = len(rows[0]) if rows else 0
    auto_column_names = [f"col_{i}" for i in range(num_columns)]
    
    with open(output_file2, 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(auto_column_names)
        for row in rows:
            writer.writerow(row)
    
    print(f"输出文件: {output_file2}")
    with open(output_file2, 'r', encoding='utf-8') as f:
        content = f.read()
        print(content)
    
    # 演示3: 读取并显示处理后的数据
    print("\n演示3: 读取处理后的数据")
    print(f"从 {output_file1} 读取数据:")
    
    with open(output_file1, 'r', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for i, row in enumerate(reader, 1):
            print(f"行 {i}: {dict(row)}")
    
    # 清理演示文件
    if os.path.exists(output_file1):
        os.remove(output_file1)
    if os.path.exists(output_file2):
        os.remove(output_file2)
    
    print("\n=== 演示完成 ===")
    print("CSVHeaderManager 类已成功添加到 data_analysis.py 文件中！")
    print("您可以在实际项目中使用此类来处理没有列名的CSV文件。")

if __name__ == "__main__":
    demo_csv_header_manager()